#!/usr/bin/perl
use strict;
use warnings;
use FindBin;
use lib $FindBin::RealBin;
use Getopt::Long;
use IMBK::Session;

my %cmd_line = ( Session_Dir => 'session-dir=s',
					Clear_Categories => 'clear-categories',
					Get_Categories => 'get-categories=s' );
my %cmd_options = ( Session_Dir => Session::default_settings_dir(),
					Clear_Categories => undef,
					Get_Categories => undef );

GetOptions( $cmd_line{Session_Dir} => \$cmd_options{Session_Dir}, 
			$cmd_line{Clear_Categories} => \$cmd_options{Clear_Categories},
			$cmd_line{Get_Categories} => \$cmd_options{Get_Categories} );

my $session = Session->new( Settings_Dir => $cmd_options{Session_Dir} ) or die "Cannot load sesstion";

&clear_categories if defined $cmd_options{Clear_Categories};
&get_categories if defined $cmd_options{Get_Categories};

sub clear_categories() {
	$session->clear_categories() or die "cannot clear categories";
}

sub get_categories() {
	my @keywords = split(",",$cmd_options{Get_Categories});
	
	my %hash = $session->get_categories(@keywords);

	foreach my $key ( keys %hash ) {
		print $key . " - " . $hash{$key} . "\n" if ( defined $hash{$key} );
	}
}

